/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');


    var toastr =require('toastr');
    app.useModule("ui.table");

    var datepicker = require('datepicker');

    app.controller('invite_listCtrl', ['$scope','$http','$filter' ,'$interval',function ($scope, $http,$filter,$interval) {
        $scope.orderParams = {
            url: "/admin/invite/list"
        };
        datepicker($scope);
        $scope.ctrlScope=$scope;

        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
        $scope.selectOptions = {
            allowClear: false,
            language : 'zh-CN',
        };

        //导出投资信息
        $scope.inviteDownload=function(){
            var params={userId: $scope.downloadUserId};
            $http.post("/admin/invite/excel/inviteInvest",params)
                .success(function (data){
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){
                });
        };
        //创建
        $scope.addOrderModal=function(){
            $scope.orderModal = !$scope.orderModal ;
        };
        //投资详情查看
        $scope.orderParams1 = {
            url: "/admin/invite/getInvestListByUserId"
        };
        $scope.openViwModal=function(id) {
            $scope.viewItemModal = !$scope.viewItemModal;
            $scope.orderParams1.ajaxParam={userId:id};
            $scope.orderParams1.reload();
            $scope.downloadUserId=id;

        };


        //获取业务员列表
        $scope.getSalesmanList=function(){
            $scope.username={};
            $http({
                method: 'POST',
                url: "/admin/invite/listSalesman",
                data:{}
            }).success(function(data) {
                console.log(data)
                $scope.username=data;
            }).error(function() {
            });
        };
        //修改被邀请人
        $scope.openEditModal=function(id) {
            $scope.viewEditModal = !$scope.viewEditModal;
            $scope.getSalesmanList();
            $scope.list = {};
            $scope.Verification = {};
            $scope.Verification.getCodeTest = '获取短信验证码';
            $scope.Verification.isTimeOut = false;
            $scope.Verification.time = '';

            $scope.getCode=function(){

                $http({
                    method: 'POST',
                    url: "/admin/invite/inviteSendSms",
                    data:{},
                }).success(function(data) {
                    console.info(data);
                    if(data.result=='success'){

                        $scope.Verification.getCodeTest='重新发送';
                        $scope.Verification.time=60;
                        $scope.Verification.isTimeOut=true;

                        $interval(function () {
                            if($scope.Verification.time==0){
                                $scope.Verification.isTimeOut=false;
                                $scope.Verification.getCodeTest='获取短信验证码';
                                $scope.Verification.time='';
                            }else{
                                $scope.Verification.time--;
                            }
                        },1000);
                    }else{
                        toastr.error('发送失败');
                        $scope.Verification.isTimeOut=false;
                        $scope.Verification.getCodeTest='获取短信验证码';
                        $scope.Verification.time='';
                    }

                }).error(function(data) {
                    toastr.error('通讯失败')
                });
            };
            $scope.submitForm= function () {
                $http({
                    method: 'POST',
                    url: "/admin/invite/modifyInvite",
                    data:{
                        userId:$scope.list.userId,
                        inviteId:id,
                        inputRandomCode:$scope.list.inputRandomCode
                    }
                }).success(function(data) {
                    console.info(data);
                    if(data.result=='success'){
                        toastr.success('修改成功');
                        $scope.viewEditModal = !$scope.viewEditModal;
                        $scope.orderParams.reload();

                    }else{
                        toastr.error(data.messageText);
                        $scope.Verification.isTimeOut2=false;
                        $scope.Verification.getCodeTest2='获取短信验证码';
                        $scope.Verification.time2='';
                    }

                }).error(function(data) {});
            };


        };


        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime=$scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime=$scope.ctrlScope.itemsearch.endDateTimex;
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={};
            $scope.orderParams.reload();
        };

        //导出
        $scope.download=function(){
            var params=$scope.ctrlScope.itemsearch;
            $http.post("/admin/invite/excel/invite",params)
                .success(function (data){
                    console.info(data);
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };
    }]);


});